{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18 <class 'int'>\n",
      "18 <class 'numpy.ndarray'>\n",
      "()\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'int' object has no attribute 'shape'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 11\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[38;5;28mprint\u001b[39m(scalar_value,\u001b[38;5;28mtype\u001b[39m(scalar_value))\n\u001b[0;32m      9\u001b[0m \u001b[38;5;28mprint\u001b[39m(scalar_value\u001b[38;5;241m.\u001b[39mshape)\n\u001b[1;32m---> 11\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mint_value\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mshape\u001b[49m)\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'int' object has no attribute 'shape'"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "int_value = 18\n",
    "print(int_value,type(int_value))\n",
    "\n",
    "scalar_value = np.array(int_value)\n",
    "print(scalar_value,type(scalar_value))\n",
    "\n",
    "print(scalar_value.shape)\n",
    "\n",
    "print(int_value.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], [4, 5, 6]] <class 'list'>\n",
      "[[1 2 3]\n",
      " [4 5 6]] <class 'numpy.ndarray'>\n",
      "(2, 3)\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'shape'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 15\u001b[0m\n\u001b[0;32m     12\u001b[0m \u001b[38;5;28mprint\u001b[39m(matrix_value\u001b[38;5;241m.\u001b[39mshape)\n\u001b[0;32m     14\u001b[0m \u001b[38;5;66;03m# 对列表 使用 ndarray.shape 方法会报错\u001b[39;00m\n\u001b[1;32m---> 15\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mlist_value\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mshape\u001b[49m)\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'shape'"
     ]
    }
   ],
   "source": [
    "\n",
    "list_value = [[1, 2, 3], [4, 5, 6]]\n",
    "print(list_value, type(list_value))\n",
    "\n",
    "matrix_value = np.array(list_value)\n",
    "print(matrix_value, type(matrix_value))\n",
    "\n",
    "print(matrix_value.shape)\n",
    "\n",
    "print(list_value.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]] \n",
      "\n",
      " <class 'list'> \n",
      "\n",
      "\n",
      "[[[ 1  2  3]\n",
      "  [ 4  5  6]]\n",
      "\n",
      " [[ 7  8  9]\n",
      "  [10 11 12]]] \n",
      "\n",
      " <class 'numpy.ndarray'>\n",
      "(2, 2, 3)\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'shape'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 10\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;28mprint\u001b[39m(array_value, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28mtype\u001b[39m(array_value))\n\u001b[0;32m      8\u001b[0m \u001b[38;5;28mprint\u001b[39m(array_value\u001b[38;5;241m.\u001b[39mshape)\n\u001b[1;32m---> 10\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mlist_value\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mshape\u001b[49m)\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'shape'"
     ]
    }
   ],
   "source": [
    "\n",
    "list_value = [[[1, 2, 3], [4, 5, 6]],\n",
    "              [[7, 8, 9], [10, 11, 12]]]\n",
    "print(list_value, \"\\n\\n\", type(list_value), \"\\n\\n\")\n",
    "\n",
    "array_value = np.array(list_value)\n",
    "print(array_value, \"\\n\\n\", type(array_value))\n",
    "\n",
    "print(array_value.shape)\n",
    "\n",
    "print(list_value.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "行向量\n",
      " [[1 2 3]] shape= (1, 3) \n",
      "\n",
      "列向量\n",
      " [[4]\n",
      " [5]\n",
      " [6]] shape= (3, 1)\n"
     ]
    }
   ],
   "source": [
    "vector_row = np.array([[1, 2, 3]])\n",
    "print(\"行向量\\n\", vector_row, 'shape=', vector_row.shape, \"\\n\")\n",
    "\n",
    "vector_column = np.array([[4], [5], [6]])\n",
    "print(\"列向量\\n\", vector_column, 'shape=', vector_column.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "行向量\n",
      " [[1 2 3]] shape= (1, 3)\n",
      "转置后\n",
      " [[1]\n",
      " [2]\n",
      " [3]] Tshape= (3, 1) \n",
      "\n",
      "列向量\n",
      " [[4]\n",
      " [5]\n",
      " [6]] shape= (3, 1)\n",
      "转置后\n",
      " [[4 5 6]] Tshape= (1, 3) \n",
      "\n"
     ]
    }
   ],
   "source": [
    "vector_row = np.array([[1, 2, 3]])\n",
    "print(\"行向量\\n\", vector_row, 'shape=', vector_row.shape)\n",
    "print(\"转置后\\n\", vector_row.T, 'Tshape=', vector_row.T.shape, \"\\n\")\n",
    "vector_column = np.array([[4], [5], [6]])\n",
    "print(\"列向量\\n\", vector_column, 'shape=', vector_column.shape)\n",
    "print(\"转置后\\n\", vector_column.T, 'Tshape=', vector_column.T.shape, \"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]] shape= (2, 3)\n",
      "[[ 2  4  6]\n",
      " [ 8 10 12]] shape= (2, 3)\n",
      "[[3 4 5]\n",
      " [6 7 8]] shape= (2, 3)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "matrix_a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(matrix_a, 'shape=', matrix_a.shape)\n",
    "\n",
    "# 矩阵*标量\n",
    "matrix_b = matrix_a * 2\n",
    "print(matrix_b, 'shape=', matrix_b.shape)\n",
    "\n",
    "# 矩阵+标量\n",
    "matrix_c = matrix_a + 2\n",
    "print(matrix_c, 'shape=', matrix_c.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  0  0]\n",
      " [ 0 10  0]]\n",
      "[[ 2  4  6]\n",
      " [ 8  0 12]]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "matrix_a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "matrix_b = np.array([[-1, -2, -3], [-4, 5, -6]])\n",
    "\n",
    "print(matrix_a + matrix_b)\n",
    "print(matrix_a - matrix_b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ -1  -4  -9]\n",
      " [-16  25 -36]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ -1,  -4,  -9],\n",
       "       [-16,  25, -36]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "matrix_b = np.array([[-1, -2, -3], [-4, 5, -6]])\n",
    "\n",
    "print(matrix_a * matrix_b)  \n",
    "np.multiply(matrix_a, matrix_b) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "operands could not be broadcast together with shapes (3,3) (2,3) ",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[9], line 4\u001b[0m\n\u001b[0;32m      1\u001b[0m matrix_a \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([[\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m3\u001b[39m], [\u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m6\u001b[39m], [\u001b[38;5;241m7\u001b[39m, \u001b[38;5;241m8\u001b[39m, \u001b[38;5;241m9\u001b[39m]])\n\u001b[0;32m      2\u001b[0m matrix_b \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([[\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m3\u001b[39m], [\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m5\u001b[39m, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m6\u001b[39m]])\n\u001b[1;32m----> 4\u001b[0m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmultiply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmatrix_a\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmatrix_b\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (3,3) (2,3) "
     ]
    }
   ],
   "source": [
    "matrix_a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "matrix_b = np.array([[-1, -2, -3], [-4, 5, -6]])\n",
    "\n",
    "np.multiply(matrix_a, matrix_b)  # 报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14, 16, 10, 10],\n",
       "       [32, 37, 28, 28]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "matrix_b = np.array([[1, 2, 3, 4], [2, 1, 2, 0], [3, 4, 1, 2]])\n",
    "\n",
    "np.matmul(matrix_a, matrix_b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[14]\n",
      " [32]]\n",
      "\n",
      " [[39 54 69]]\n"
     ]
    }
   ],
   "source": [
    "matrix_a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "matrix_b = np.array([[1], [2], [3]]) \n",
    "matrix_c = np.array([[7, 8]]) \n",
    "\n",
    "\n",
    "print(np.matmul(matrix_a, matrix_b))\n",
    "\n",
    "\n",
    "print(\"\\n\", np.matmul(matrix_c, matrix_a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2,) (2,)\n"
     ]
    }
   ],
   "source": [
    "matrix_d = np.array([7, 8]) \n",
    "matrix_e = matrix_d.T  \n",
    "print(matrix_d.shape, matrix_e.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5],\n",
       "       [7],\n",
       "       [9]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matrix_a = np.array([[1], [2], [3]])\n",
    "matrix_b = np.array([[4], [5], [6]])\n",
    "\n",
    "matrix_a + matrix_b\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  4  6]\n",
      " [ 4  8 12]\n",
      " [-1 -2 -3]] \n",
      "\n",
      "[[7]]\n"
     ]
    }
   ],
   "source": [
    "matrix_a = np.array([[2], [4], [-1]])\n",
    "matrix_b = np.array([[1, 2, 3]])  \n",
    "\n",
    "print(np.matmul(matrix_a, matrix_b), '\\n')\n",
    "\n",
    "print(np.matmul(matrix_b, matrix_a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]] shape= (2, 3) \n",
      "\n",
      " [[1 4]\n",
      " [2 5]\n",
      " [3 6]] Tshape= (3, 2)\n"
     ]
    }
   ],
   "source": [
    "matrix_a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "print(matrix_a, 'shape=', matrix_a.shape, '\\n\\n', matrix_a.T, 'Tshape=', matrix_a.T.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]] shape= (1, 3) \n",
      "\n",
      " [[1]\n",
      " [2]\n",
      " [3]] .Tshape= (3, 1)\n"
     ]
    }
   ],
   "source": [
    "vector_row = np.array([[1, 2, 3]])\n",
    "\n",
    "print(vector_row, 'shape=', vector_row.shape, '\\n\\n', vector_row.T, '.Tshape=', vector_row.T.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1.0\n",
       "1    3.0\n",
       "2    5.0\n",
       "3    NaN\n",
       "4    6.0\n",
       "5    8.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series([1, 3, 5, np.nan, 6, 8])\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "洗牌前的\n",
      "x_=[11. 12. 13. 14. 15. 16. 17. 18. 19. 20.],\n",
      "y_=[21. 22. 23. 24. 25. 26. 27. 28. 29. 30.],\n",
      "z_=[31. 32. 33. 34. 35. 36. 37. 38. 39. 40.]\n",
      "\n",
      "第1轮的\n",
      "x_=[16. 20. 11. 12. 13. 17. 15. 18. 19. 14.],\n",
      "y_=[26. 30. 21. 22. 23. 27. 25. 28. 29. 24.],\n",
      "z_=[39. 32. 33. 31. 36. 35. 34. 40. 37. 38.]\n",
      "\n",
      "第2轮的\n",
      "x_=[18. 12. 16. 17. 20. 11. 19. 15. 13. 14.],\n",
      "y_=[28. 22. 26. 27. 30. 21. 29. 25. 23. 24.],\n",
      "z_=[36. 31. 34. 32. 38. 35. 37. 40. 33. 39.]\n",
      "\n",
      "第3轮的\n",
      "x_=[19. 12. 20. 14. 15. 11. 18. 16. 13. 17.],\n",
      "y_=[29. 22. 30. 24. 25. 21. 28. 26. 23. 27.],\n",
      "z_=[38. 35. 33. 32. 31. 40. 36. 34. 37. 39.]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.utils import shuffle\n",
    "import numpy as np\n",
    "\n",
    "d_ = np.linspace(1, 10, 10)\n",
    "x_ = d_ + 10\n",
    "y_ = d_ + 20\n",
    "z_ = d_ + 30\n",
    "\n",
    "print(f'洗牌前的\\nx_={x_},\\ny_={y_},\\nz_={z_}\\n')\n",
    "for epoch in range(3):\n",
    "    z_ = shuffle(z_)\n",
    "    x_, y_ = shuffle(x_, y_)\n",
    "    print(f'第{epoch+1}轮的\\nx_={x_},\\ny_={y_},\\nz_={z_}\\n')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tensorflow",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
